package com.wellsoft.mobileoa.utils.view;

import java.util.ArrayList;
import java.util.List;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListSet;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListSubItem;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.ServiceResponse;

public class RadioView extends BaseView {

	public ListView radiolv;
	public RadioViewAdapter radioAdp;
	public RadioGroup radioGroup;

	public RadioView(BaseActivity baseActivity, WSOA_Page page, String url) {
		super(baseActivity);
		this.baseActivity = baseActivity;
		this.page = page;
		this.url = url;
	}

	@Override
	public ViewGroup DrawSelf() {
		if (page.getPageListSet().getListItems() == null) {
			return null;
		}
		List<WSOA_PageList> pageLists = new ArrayList<WSOA_PageList>();
		WSOA_PageListSet pageListSet = page.getPageListSet();
		for (int i = 0; i < pageListSet.getListItems().size(); i++) {
			for (int j = 0; j < pageListSet.getListItems().get(i)
					.getListSubItems().size(); j++) {
				WSOA_PageList pageList = new WSOA_PageList();
				pageList.setPageListItem(pageListSet.getListItems().get(i));
				pageList.setPageListSubItem(pageListSet.getListItems().get(i)
						.getListSubItems().get(j));
				pageLists.add(pageList);
			}
		}

		radioAdp = new RadioViewAdapter(pageLists, baseActivity);
		selfView = (ViewGroup) LayoutInflater.from(baseActivity).inflate(
				R.layout.radioview, null);

		radiolv = (ListView) selfView.findViewById(R.id.radioview_radiolv);

		initTitleBar(selfView);

		radiolv.setAdapter(radioAdp);
		
		radiolv.setOnItemClickListener(onRadioItemClickListener);

		initToolBar(selfView);

		return selfView;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			backOnClickListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public List<WSOA_PostField> getValue() {
		List<WSOA_PostField> postFields = new ArrayList<WSOA_PostField>();
		WSOA_PostField postField = new WSOA_PostField();
		postField.setFieldName(page.getPageMenus().get(0).getAction()
				.getListfield());
		postField.setFieldValues(radioAdp.getCheckedValues());

		postFields.add(postField);

		return postFields;
	}

	class WSOA_PageList {
		public WSOA_PageListSubItem pageListSubItem;
		public WSOA_PageListItem pageListItem;

		public WSOA_PageListSubItem getPageListSubItem() {
			return pageListSubItem;
		}

		public void setPageListSubItem(WSOA_PageListSubItem pageListSubItem) {
			this.pageListSubItem = pageListSubItem;
		}

		public WSOA_PageListItem getPageListItem() {
			return pageListItem;
		}

		public void setPageListItem(WSOA_PageListItem pageListItem) {
			this.pageListItem = pageListItem;
		}

	}

	@Override
	public ServiceResponse checkValid() {
		if (radioAdp.selectedIndex.size() > 0) {
			return new ServiceResponse(true);
		} else {
			return new ServiceResponse(false, "请选择一行记录！");
		}
	}

	OnItemClickListener onRadioItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			radioAdp.selectItem(arg2);
		}
	};
}
